//
//  IndexController.m
//  linlee-iotios
//
//  Created by mengze on 2025/5/25.
//

#import <Foundation/Foundation.h>
#import "Masonry.h"
#import "UcenterController.h"
#import "UIColor+Hex.h"

//#import <SDWebImage/SDWebImage.h>


@interface UcenterController()

@property( nonatomic, strong ) UIView * mainView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) UIImageView *avatarImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *idLabel;

@property (nonatomic, strong) UIView *statsContainerView;
@property (nonatomic, strong) UILabel *todayOrdersLabel;
@property (nonatomic, strong) UILabel *monthIncomeLabel;
@property (nonatomic, strong) UILabel *serviceRatingLabel;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSArray *itemTitles;

@end


@implementation UcenterController

//- (UIStatusBarStyle)preferredStatusBarStyle {
//    return UIStatusBarStyleLightContent;
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"个人中心"];


    //用于控制导航栏中状态的背景颜色。
    self.mainView = [[UIView alloc] init];
    [self.mainView  setBackgroundColor:[UIColor colorWithHexString:@"#1E90EF"]];
    self.mainView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview: self.mainView ];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideTop);
    }];
    

    [self setupHeaderView];
    [self setupUI];
    
    [self setupContentView];
    [self createItems];
    
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#f5f5f5"]];
}


- (void)setupContentView {
    _contentView = [[UIView alloc] init];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.layer.cornerRadius = 8;
    _contentView.layer.masksToBounds = YES;
    [self.view addSubview:_contentView];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.statsContainerView.mas_bottom).offset(12);
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
    }];
}


- (void)createItems {
    UIView *lastView = nil;
    
    self.itemTitles = @[@"我的订单", @"收入明细", @"设置"];
    for (NSInteger i = 0; i < self.itemTitles.count; i++) {
        UIView *itemView = [self createItemViewWithTitle:self.itemTitles[i] index:i];
        [_contentView addSubview:itemView];
        
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_contentView);
            make.height.mas_equalTo(44);
            
            if (lastView) {
                make.top.equalTo(lastView.mas_bottom);
            } else {
                make.top.equalTo(_contentView);
            }
            
            if (i == self.itemTitles.count - 1) {
                make.bottom.equalTo(_contentView);
            }
        }];
        
        lastView = itemView;
    }
}

- (UIView *)createItemViewWithTitle:(NSString *)title index:(NSInteger)index {
    UIView *itemView = [[UIView alloc] init];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [itemView addSubview:titleLabel];
    
    UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]];
    [itemView addSubview:arrow];
    
    if (index < self.itemTitles.count - 1) {
        UIView *separator = [[UIView alloc] init];
        separator.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        [itemView addSubview:separator];
        
        [separator mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(itemView);
            make.height.mas_equalTo(0.5);
        }];
    }
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(itemView).offset(16);
        make.centerY.equalTo(itemView);
    }];
    
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(itemView).offset(-16);
        make.centerY.equalTo(itemView);
        make.width.height.mas_equalTo(16);
    }];
    
    return itemView;
}



- (void)updateTodayOrders:(NSString *)value {
    [self updateLabel:self.todayOrdersLabel withValue:value];
}

- (void)updateMonthIncome:(NSString *)value {
    [self updateLabel:self.monthIncomeLabel withValue:value];
}

- (void)updateServiceRating:(NSString *)value {
    [self updateLabel:self.serviceRatingLabel withValue:value];
}

- (void)updateLabel:(UILabel *)label withValue:(NSString *)value {
    NSMutableAttributedString *attString = [label.attributedText mutableCopy];
    [attString replaceCharactersInRange:NSMakeRange(attString.length - 2, 2)
                  withAttributedString:[[NSAttributedString alloc] initWithString:value
                                                                      attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
                                                                                   NSForegroundColorAttributeName:[UIColor blackColor]}]];
    label.attributedText = attString;
}





- (void)setupUI {

    
    // 容器视图
    self.statsContainerView = [[UIView alloc] init];
    self.statsContainerView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
    self.statsContainerView.layer.cornerRadius = 10;
    self.statsContainerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.statsContainerView];
    
    // 今日接单
    self.todayOrdersLabel = [self createStatLabelWithTitle:@"今日接单"];
    [self.statsContainerView addSubview:self.todayOrdersLabel];
    
    // 本月收入
    self.monthIncomeLabel = [self createStatLabelWithTitle:@"本月收入"];
    [self.statsContainerView addSubview:self.monthIncomeLabel];
    
    // 服务评分
    self.serviceRatingLabel = [self createStatLabelWithTitle:@"服务评分"];
    [self.statsContainerView addSubview:self.serviceRatingLabel];
    
    // Masonry布局
    [self.statsContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom).offset(12);
        make.left.right.equalTo(self.view).inset(12);
        make.height.equalTo(@70);
    }];
    
    [self.todayOrdersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.statsContainerView).offset(15);
        make.left.equalTo(self.statsContainerView).offset(20);
        make.width.equalTo(self.statsContainerView).multipliedBy(0.3);
    }];
    
    [self.monthIncomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.todayOrdersLabel);
        make.centerX.equalTo(self.statsContainerView);
        make.width.equalTo(self.todayOrdersLabel);
    }];
    
    [self.serviceRatingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.todayOrdersLabel);
        make.right.equalTo(self.statsContainerView).offset(-20);
        make.width.equalTo(self.todayOrdersLabel);
    }];
}

- (UILabel *)createStatLabelWithTitle:(NSString *)title {
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    label.numberOfLines = 2;
    
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n", title]
                                                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                                                                 NSForegroundColorAttributeName:[UIColor grayColor]}]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"--"
                                                                    attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
                                                                                 NSForegroundColorAttributeName:[UIColor blackColor]}]];
    label.attributedText = attString;
    
    return label;
}


- (void)setupHeaderView {
    // 1. 创建头部容器
    _headerView = [UIView new];
    _headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_headerView];
    

    // 3. 头像布局
    _avatarImage = [UIImageView new];
    _avatarImage.backgroundColor = [UIColor lightGrayColor];
    _avatarImage.layer.cornerRadius = 30;
    _avatarImage.clipsToBounds = YES;
    [_headerView addSubview:_avatarImage];
    

    
    // 4. 姓名标签
    _nameLabel = [UILabel new];
    _nameLabel.text = @"用户名";
    _nameLabel.font = [UIFont boldSystemFontOfSize:18];
    [_headerView addSubview:_nameLabel];
    

    
    // 5. ID标签
    _idLabel = [UILabel new];
    _idLabel.text = @"ID: 123456789";
    _idLabel.textColor = [UIColor grayColor];
    _idLabel.font = [UIFont systemFontOfSize:14];
    [_headerView addSubview:_idLabel];
    
    
    // 2. Masonry约束布局
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.mas_equalTo(90);
    }];
    
    
    [_avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headerView).offset(15);
        make.centerY.equalTo(_headerView);
        make.width.height.mas_equalTo(60);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarImage.mas_right).offset(15);
        make.top.equalTo(_avatarImage).offset(5);
    }];
    
    [_idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(_nameLabel.mas_bottom).offset(5);
    }];
}

@end
